package org.hu.service;

import org.hu.mapper.EmployeeMapper;
import org.hu.mapper.MeetingParticipantsMapper;
import org.hu.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    MeetingParticipantsMapper meetingParticipantsMapper;


    /**
     * 执行登录操作
     * @param username 用户名
     * @param password 密码
     * @return Employee
     */
    public Employee dologin(String username, String password) {
        Employee employee = employeeMapper.loadEmpByUsername(username);
        if(employee==null||!(employee.getPassword().equals(password))) {return null;}
        return employee;
    }

    /**
     * 执行注册
     * @param employee 员工对象
     * @return Integer 0为账户已存在 1为成功
     */
    public Integer doReg(Employee employee) {
        employee.setRole(1);
        employee.setStatus(0);
        if(employeeMapper.loadEmpByUsername(employee.getUsername())!=null){
            return 0;
        }
        return employeeMapper.doReg(employee);
    }

    /**
     * 通过状态查找员工
     * @param status 状态 0为待批准 1为批准通过 2为拒绝
     * @return List<Employee>
     */
    public List<Employee> getEmployeeByStatus(Integer status){
        return employeeMapper.getEmployeeByStatus(status);
    }

    /**
     * 更新员工状态
     * @param id 员工id
     * @param status 状态 0为待批准 1为批准通过 2为拒绝
     * @return Integer 0为失败 1为成功
     */
    public Integer updateStatus(Integer id, Integer status) {
        return employeeMapper.updateStatus(id,status);
    }

    /**
     * 取得所有员工并分页
     * @param employee 员工对象
     * @param page  页数
     * @param pageSize  每页数量
     * @return List<Employee>
     */
    public List<Employee> getAllEmployees(Employee employee, Integer page, Integer pageSize) {
        page=(page-1)*pageSize;
        return employeeMapper.getAllEmployee(employee,page,pageSize);
    }

    /**
     * 获得所有员工的数量
     * @param employee
     * @return Long
     */
    public Long getTotal(Employee employee) {
        return employeeMapper.getTotal(employee);
    }

    /**
     * 查询部门所属员工
     * @param depId 部门编号
     * @return List<Employee>
     */
    public  List<Employee> getEmpByDep(Integer depId) {
        return employeeMapper.getEmpByDep(depId);
    }

    /**
     * 通过会议id查询参加的员工
     * @param meetingid
     * @return List<Employee>
     */
    public List<Employee> getEmpsByid(Integer meetingid) {
        List<Integer> empids = meetingParticipantsMapper.getEmpsByid(meetingid);
        return employeeMapper.getEmpByid(empids);
    }

    /**
     * 修改密码
     * @param password 用户输入的密码
     * @param newpassowrd 新密码
     * @param username 用户名
     * @param truepassword 正确的密码
     * @return Integer -1为密码错误 -2为新密码与旧密码相同 1为成功
     */
    public Integer updatepwd(String password, String newpassowrd,String username,String truepassword) {
        if (!(truepassword.equals(password))){return -1;}
        else if(newpassowrd.equals(password)){return -2;}
        else {return employeeMapper.updatepwd(username,newpassowrd);}
    }
}
